package com.liuwei.msmall.seckill.controller;

import com.liuwei.msmall.common.utils.R;
import com.liuwei.msmall.seckill.service.SeckillService;
import com.liuwei.msmall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Classname SeckillController
 * @Date 2021/5/5 17:50
 * @Created by 刘
 */
@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    @ResponseBody
    @GetMapping("/currentSeckillSkuInfo")
    public R getCurrentSeckillSkuInfo() {

        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkuInfo();

        return R.ok().setData(vos);
    }


    //根据商品id查询对应的秒杀信息
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo vo = seckillService.getSkuSeckillInfoById(skuId);
        return R.ok().setData(vo);
    }


    /**
     * 秒杀商品
     * @param key
     * @param count
     * @param killId
     * @return
     */
    @GetMapping("/kill")
    public String seckillSku(@RequestParam("code") String key,
                             @RequestParam("num") Integer count,
                             @RequestParam("killId") String killId,
                             Model model) {

        //调用秒杀服务返回订单号即秒杀成功
        String orderSn = seckillService.kill(key, count, killId);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }

}
